package com.school.springboot_login.dao;


import com.school.springboot_login.bean.User;
import com.sun.org.apache.xml.internal.security.utils.resolver.implementations.ResolverLocalFilesystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

//通过spring注解定义一个DAO
@Repository
public class UserDao {
    //自动注入JDBC Template 的Bean
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public UserDao() {
        System.out.println("UserDao");
    }
    //找到对应用户
    public User findUserByUserName(final String userName) {
        //根据用户名查询用户的SQL语句
        String sqlStr = " SELECT user_id,user_name,credits,loginTimes,loginFailedTimes"
                + " FROM user WHERE user_name =? ";
        //sql = " SELECT * FROM user where username = '"+user.getUsername()+"' and password = '"+user.getPassword()+"'";
        final User user = new User();
        jdbcTemplate.query(sqlStr,new RowCallbackHandler()
                //匿名类方式实现的回调函数
                 {
                    public void processRow(ResultSet rs) throws SQLException {
                        user.setUserId(rs.getInt("user_id"));
                        user.setUserName(userName);
                        user.setCredits(rs.getInt("credits"));
                        user.setLoginTimes(rs.getInt("loginTimes"));
                        user.setLoginFailedTimes(rs.getInt("loginFailedTimes"));
                    }
                },new Object[]{userName});
        return user;
    }
    //匹配用户的数目
    public int getMatchCount(String userName, String password) {
        String sqlStr = "select count(*) from user where user_name = ? and password = ?";
        Object args[]=new Object[]{userName,password};
        return jdbcTemplate.queryForObject(sqlStr,int.class,new Object[] { userName,password });

    }
    //更新用户信息
    public void updateLoginInfo(User user) {
        String sqlStr = "update user set last_visit=?,last_ip=?,credits=?,loginTimes=?,loginFailedTimes=? where user_id=?";
        Object args[] = new Object[]{user.getLastVisit(), user.getLastIp(), user.getCredits(),user.getLoginTimes(),user.getLoginFailedTimes(),user.getUserId()};

        jdbcTemplate.update(sqlStr, args);
    }
    //插入用户的信息
    public void InsertUser(User user){
        String sqlInsert="insert into user(user_name,password) values (?,?)";
        //Object args[]=new Object[]{userName,password};
        jdbcTemplate.update(sqlInsert,user.getUserName(),user.getPassword());
    }

    public boolean getUserByUserNameAndPassword(String name, String password){
        String sql = "SELECT user_name,password FROM user WHERE user_name = ? AND password = ?";
        Object[] args = {name, password};
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql, args);
        System.out.println(maps);

        if(maps.size() == 0){
            return false;
        }
        return true;
    }

    public void increaseLoginTimes(String name){
        String sql = "update user set loginTimes = loginTimes + 1 where user_name = ?";
        Object[] args = {name};
        jdbcTemplate.update(sql,args);
    }

    public void increaseLoginFailedTimes(String name) {
        String sql = "update user set loginFailedTimes = loginFailedTimes + 1 where user_name = ?";
        Object[] args = {name};
        jdbcTemplate.update(sql,args);
    }

    public void registerDao(String name, String password){
        String sql = "INSERT INTO user(user_name, password, loginTimes, loginFailedTimes) values (?,?,0,0)";
        Object[] args = {name, password};
        jdbcTemplate.update(sql, args);
    }
    //查询所有用户登录成功次数和失败次数
    public List<User> getAllUserLoginTimes()
    {
        // 1.创建QueryRunner对象.
        //QueryRunner qr = new QueryRunner(DataSourceUtil.getDataSource());
        String sql = "SELECT user_name,loginTimes,loginFailedTimes FROM user ";
        List users=jdbcTemplate.query(sql,new UserMapper());
        return users;

    }
    protected class UserMapper implements RowMapper {
        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
            User user = new User();
            user.setUserName(rs.getString("user_name"));
            user.setLoginTimes(rs.getInt("loginTimes"));
            user.setLoginFailedTimes(rs.getInt("loginFailedTimes"));
            return user;
        }
    }
}
